// subPages/shopPage/details/index.js
var app = getApp();
var utils = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      currentTab:0,
      storeId:'',
      storeName:'请选择门店',
      navList:[
          {
              nav:"详情"
          },{
              nav:"参数"
          }
      ],
      goodsInfo:'',
      indicatorDots: true,
      vertical: false,
      autoplay: true,
      circular: true,
      interval: 4000,
      duration: 500,
      previousMargin: 0,
      nextMargin: 0,
      indicatorColor:"raba(255,255,255,.3)",
      indicatorActiveColor:"#ffffff",
      imgWidth:wx.getSystemInfoSync().windowWidth
  },
    //选择导航栏
    switchNav(event){
        var cur = event.currentTarget.dataset.current;
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        this.getDetailsH()
    },
    switchTab(event){
        var cur = event.detail.current;
        this.setData({
            currentTab: cur
        });
        this.getDetailsH()
    },
    //获取swiper 图片高度
  imgHeight:function(e){
        var winW = wx.getSystemInfoSync().windowWidth;
        var imgW = e.detail.width;
        var imgH = e.detail.height;
        var swiperH = winW*imgH/imgW+"px";
        console.log(swiperH,winW,imgH,imgW);
        this.setData({
            Height:swiperH
        })
    },
    //详情是swiper高度
    getDetailsH:function(){
      let that = this;
      let winW = wx.getSystemInfoSync().windowWidth;
        console.log(winW);
        let curr=0,imgW,imgH;
      if(that.data.currentTab==0){
          let imgs = that.data.goodsInfo.content_img;
          console.log(that.data.goodsInfo);
          console.log(imgs);
          for(let i=0;i<imgs.length;i++){
              wx.getImageInfo({
                src:imgs[i].url,
                success:function(res){
                   imgW = res.width;
                   imgH = res.height;
                   curr += imgH/(imgW/winW);
                    console.log(curr);
                    that.setData({
                        Sheight:curr
                    })
                }
              })
          }

      }
      if(that.data.currentTab==1){
          console.log(1);
          wx.createSelectorQuery().select('.canshu').boundingClientRect(function(res){
              console.log(res);
              curr = res.height;
              that.setData({
                  Sheight:curr
              })
          }).exec();
      }

    },
    //分享页面事件 内容设置
    onShareAppMessage:function(res){
     console.log(res);
     return{
         path:"/subPages/shopPage/details/index?id="+this.data.goodsInfo.id,
         success:function(res){
             wx.showToast({
                 title: '分享成功',
                 duration:2000
              })
          },
          fail:function(res){

          }
        }
  },
    checkImg:function(e){
      let cur = e.target.dataset.src;
      let imgArr = [];
      let goodsInfo = this.data.goodsInfo.head_img;
      for(var i=0;i<goodsInfo.length;i++){
          imgArr.push(goodsInfo[i].url);
      }
      wx.previewImage({
          current:cur,
          urls:imgArr
      })
    },
    toOrder:function(){
        if(app.globalData.userId){
            wx.setStorageSync('goodsInfo',this.data.goodsInfo);
            wx.navigateTo({
                url:"/subPages/shopPage/order/order"
            })
        }else{
            utils.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
              if (app.globalData.userInfo) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                // 在没有 open-type=getUserInfo 版本的兼容处理
                if(app.globalData.isAuthorized){
                    app.userInfoReadyCallback = res => {
                      if (res) {
                        //已授权
                        wx.redirectTo({
                          url: '/pages/login/login'
                        })
                      }else{
                        //未授权
                        wx.navigateTo({
                          url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                        })
                      }
                    }
                  }else{
                    wx.navigateTo({
                      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                    })
                  }
              }
            })
          }
    },
    addCart(){
        var shopId = this.data.goodsInfo.shop_id;
        // console.log(shopId);
        var userId = app.globalData.userId;
        // console.log(userId);
        var contentId = this.data.goodsInfo.id;
        // console.log(contentId);
        var sendData = {
            userid:userId,
            content_id:contentId,
            shop_id:shopId,
            num:1
        }
        utils.postAjax(app.globalData.apiUrl+'/add_buy',sendData,function(res){
                console.log(res)
                if(res.data == 1){
                    wx.showToast({
                        title:'添加成功！购物车等你哦！',
                        icon:'none',
                        duration: 2000
                    })
                }
            })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      let goodsId = options.id;
      utils.sendAjax(app.globalData.apiUrl+'/get_content','GET',this.setGoodsContent,{id:goodsId})


  },
    setGoodsContent(res){
      var that = this;
        console.log(res);
        that.setData({
          goodsInfo:res.data
      })
      that.getDetailsH();
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
